home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
E-Z Progress Bar 1.0a
/
ProgressClass.cp
< prev
next >
Wrap
Text File
|
1995-03-15
|
12KB
|
622 lines
#include "ProgressClass.h"
ProgressClass::ProgressClass(short options)
{
cancelButton=nil;
appleMenu=menu1=menu2=menu3=menu4=menu5=menu6=0;
minValue=maxValue=curValue=0.0;
SetRect(&windowRect,wLeft, wTop, wRight, wBottom);
theWindow=(WindowPtr) NewPtr(sizeof (WindowRecord));
theWindow=NewCWindow(theWindow, &windowRect, "\p", 0, noGrowDocProc, (WindowPtr) -1L, 0, 0L);
kBarHeight=11;
kBarWidth=170;
Point colorPoint;
colorPoint.h=140;
colorPoint.v=90;
// emptyColor=&qd.rgbHiliteColor; // This should work, but doesn't. If you know how
// to get the hilite color, email me!
emptyColor.red=emptyColor.blue=emptyColor.green=0xFFFF;
progressColor.red=0x2C1B;
progressColor.green=0x2A25;
progressColor.blue=0xE159;
windowColor.red=windowColor.green=windowColor.blue=0xEEEE;
progChisel.LiteDarkFrom(progressColor);
progChisel.SetChiselWay(!chiselIn);
useRGB=RGBAvailable();
SetOptions(options);
PositionElements("\p");
}
ProgressClass::~ProgressClass()
{
HiliteMenus();
if (cancelButton)
DisposeControl(cancelButton);
if (theWindow) {
CloseWindow(theWindow);
DisposPtr((Ptr) theWindow);
}
}
void ProgressClass::SetOptions(short options)
{
cancel=options & canCancel;
chiselF=options & chiselFrame;
chiselP=options & chiselBar;
grayWin=options & grayBack;
chiselH=options & fineChisel;
if (!cancel && cancelButton)
HideControl(cancelButton);
else if (cancel && !cancelButton)
SetCancelButton();
}
void ProgressClass::SetMinMax(short min, short max)
{
maxValue=max;
minValue=min;
curValue=minValue;
}
void ProgressClass::SetControlID(short controlID)
{
if (controlID)
cancel=1;
else
cancel=0;
SetCancelButton(controlID);
}
void ProgressClass::SetTexts(Str255 windowTitle, Str255 paramText)
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
SetWTitle(theWindow, windowTitle);
PositionElements(paramText);
InvalRgn(theWindow->visRgn);
SetPort(oldPort);
}
void ProgressClass::SetAppleMenu(short appleID)
{
MenuHandle theMenu=GetMHandle(appleID);
if (theMenu) {
short items=CountMItems(theMenu);
Str255 theItem;
Boolean found=0;
for(short i=1; i<=items && !found; i++) {
GetItem(theMenu, i, theItem);
if (theItem[1]=='-')
found=1;
}
appleDivider=(found) ? i-1 : 0;
}
appleMenu=appleID;
}
void ProgressClass::SetMenus(short m1, short m2, short m3, short m4, short m5, short m6, short m7, short m8)
{
menu1=m1;
menu2=m2;
menu3=m3;
menu4=m4;
menu5=m5;
menu6=m6;
menu7=m7;
menu8=m8;
}
void ProgressClass::SetCancelButton(short controlID)
{
if (cancelButton) {
DisposeControl(cancelButton);
cancelButton=nil;
}
if (cancel) {
short right=theWindow->portRect.right-20;
short bottom=theWindow->portRect.bottom-15;
SetRect(&controlRect, right-70, bottom-20, right, bottom);
if (controlID) {
cancelButton=GetNewControl(controlID, theWindow);
if (!cancelButton)
cancelButton=NewControl(theWindow, &controlRect, "\pCancel", 1, 0, 0, 1, pushButProc, 0L);
MoveControl(cancelButton, controlRect.left, controlRect.top);
}
else
cancelButton=NewControl(theWindow, &controlRect, "\pCancel", 1, 0, 0, 1, pushButProc, 0L);
ShowControl(cancelButton);
}
}
void ProgressClass::SetBar(short current)
{
curValue=current;
DrawProgressBar();
}
void ProgressClass::SetBar(short current, Str255 paramText)
{
ReplaceText(paramText);
SetBar(current);
}
void ProgressClass::SetBar(float current)
{
current=(current<0.0) ? 0.0 : current;
current=(current>1.0) ? 1.0 : current;
if ((maxValue-minValue)>0.00001) { // Use this to account for float errors when "==" is used
maxValue=100;
minValue=0;
}
curValue=minValue+current*maxValue;
DrawProgressBar();
}
void ProgressClass::SetBar(float current, Str255 paramText)
{
ReplaceText(paramText);
SetBar(current);
}
void ProgressClass::SetBarWidth(short width)
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
kBarWidth=width;
PositionElements(progressText);
InvalRgn(theWindow->visRgn);
SetPort(oldPort);
}
void ProgressClass::SetBarHeight(short height)
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
kBarHeight=height;
PositionElements(progressText);
InvalRgn(theWindow->visRgn);
SetPort(oldPort);
}
void ProgressClass::SetWindowColor(RGBColor theColor)
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
RGBBackColor(&theColor);
InvalRgn(theWindow->visRgn);
SetPort(oldPort);
}
void ProgressClass::PositionElements(Str255 paramText)
{
// Rectangle Positioning
FontInfo theFont;
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
GetFontInfo(&theFont);
short left=(theWindow->portRect.right-theWindow->portRect.left)/2-kBarWidth/2;
short top=(theWindow->portRect.bottom-theWindow->portRect.top)/2-kBarHeight/2-3;
if (chiselH)
top+=4; // Fudge factor
top+=2; // Fudge factor again
SetRect(&emptyBar, left, top, left+kBarWidth, top+kBarHeight);
frameBar=emptyBar;
InsetRect(&frameBar, -1, -1);
if (chiselP)
progressBar=progChisel.PreChisel(emptyBar);
else
progressBar=emptyBar;
pBarWidth=progressBar.right-progressBar.left;
if (chiselF)
frameChisel.SetDefaultRect(frameBar);
if (chiselH) {
Rect hairRect=frameBar;
InsetRect(&hairRect, -8, -8);
hairChisel.SetDefaultRect(hairRect);
}
// Text Positioning
short paramLength=StringWidth(paramText);
Rect tempRect;
if (chiselF)
tempRect=frameChisel.ChiselRect(frameBar);
else
tempRect=frameBar;
top=tempRect.top-(theFont.ascent-theFont.leading*2);
if (chiselH)
top-=2;
textPoint.h=tempRect.left;
textPoint.v=top;
maxCharWid=theFont.widMax;
for (short i=0; i<=paramText[0]; i++)
progressText[i]=paramText[i];
SetPort(oldPort);
}
void ProgressClass::ShowBarWindow()
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
if (grayWin && useRGB)
RGBBackColor(&windowColor);
else
BackColor(whiteColor);
InvalRect(&theWindow->portRect);
ShowWindow(theWindow);
UpdateWindow();
SetPort(oldPort);
}
void ProgressClass::HideBarWindow()
{
HideWindow(theWindow);
HiliteMenus();
}
void ProgressClass::UpdateWindow()
{
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
BeginUpdate(theWindow);
EraseRect(&theWindow->portRect);
DrawProgressText();
FrameRect(&frameBar);
if (chiselF)
frameChisel.Chisel();
if (chiselH)
hairChisel.HairChisel();
DrawProgressBar();
if (cancel)
UpdateControls(theWindow, theWindow->visRgn);
EndUpdate(theWindow);
SetPort(oldPort);
}
void ProgressClass::DrawProgressBar()
{
RGBColor backColor, foreColor;
PenState oldState;
GrafPtr oldPort;
GetPort(&oldPort);
SetPort(theWindow);
GetPenState(&oldState);
PenSize(1,1);
PenMode(patCopy);
if (curValue>maxValue)
curValue=maxValue;
else if (curValue<minValue)
curValue=minValue;
progressBar.right=progressBar.left+pBarWidth*(curValue-minValue)/maxValue;
if (progressBar.right>progressBar.left)
emptyBar.left=progressBar.right;
if (useRGB) {
GetBackColor(&backColor);
GetForeColor(&foreColor);
RGBBackColor(&emptyColor);
EraseRect(&emptyBar);
RGBBackColor(&progressColor);
EraseRect(&progressBar);
if (chiselP && (progressBar.right>progressBar.left))
progChisel.Chisel(progressBar);
}
ForeColor(blackColor);
emptyBar.left=progressBar.left;
if (useRGB) {
RGBBackColor(&backColor);
RGBForeColor(&foreColor);
}
SetPenState(&oldState);
SetPort(oldPort);
}
void ProgressClass::DrawProgressText()
{
TextFont(0);
MoveTo(textPoint.h, textPoint.v);
DrawString(progressText);
}
void ProgressClass::ReplaceText(Str255 paramText)
{
FontInfo theFont;
GrafPtr oldPort;
short i, numEraseChars, curWidth;
Rect tempRect;
GetPort(&oldPort);
SetPort(theWindow);
GetFontInfo(&theFont);
for (i=1; (i<=progressText[0]) && (paramText[i]==progressText[i]); i++)
;
numEraseChars=progressText[0]-i+1;
curWidth=StringWidth(progressText);
SetRect(&tempRect, textPoint.h+(curWidth-numEraseChars*maxCharWid), textPoint.v-(theFont.descent+theFont.ascent), textPoint.h+curWidth, textPoint.v+theFont.descent);
EraseRect(&tempRect);
for (i=0; i<=paramText[0]; i++)
progressText[i]=paramText[i];
DrawProgressText();
SetPort(oldPort);
}
Boolean ProgressClass::ReceivedEvent(Boolean *userCancel, Boolean *deactivate, WindowPtr *updateWindow)
{
if (WaitNextEvent(everyEvent, &theEvent, 10, nil)) {
switch (theEvent.what) {
case keyDown:
if (((theEvent.message & charCodeMask) == '.') && (theEvent.modifiers & cmdKey) && cancel) {
HiliteControl(cancelButton, inButton);
Delay(8, nil);
HiliteControl(cancelButton, 0);
*userCancel=1;
*deactivate=0;
*updateWindow=nil;
return 1;
}
else
return 0;
break;
case mouseDown:
WindowPtr thisWindow;
short part=FindWindow(theEvent.where, &thisWindow);
GrafPtr oldPort;
Point where=theEvent.where;
GetPort(&oldPort);
SetPort(theWindow);
GlobalToLocal(&where);
SetPort(oldPort);
switch (part) {
case inContent:
if (thisWindow==theWindow) {
if (FindControl(where, theWindow, nil))
if (TrackControl(cancelButton, where, nil)) {
*userCancel=1;
*deactivate=0;
*updateWindow=nil;
return 1;
}
}
else
SysBeep(10);
return 0;
break;
case inDrag:
if (thisWindow==theWindow) {
RgnHandle grayRgn=GetGrayRgn();
DragWindow(theWindow, theEvent.where, &(**grayRgn).rgnBBox);
}
return 0;
break;
case inMenuBar:
long menuSelection;
DimMenus();
menuSelection=MenuSelect(theEvent.where);
HiliteMenu(0);
short menu=HiWord(menuSelection);
short item=LoWord(menuSelection);
if (appleMenu==menu) {
if (item<=appleDivider)
SysBeep(10);
else {
Str255 itemString;
GetItem(GetMHandle(appleMenu), item, itemString);
OpenDeskAcc(itemString);
}
}
return 0;
break;
default:
return 0;
break;
}
break;
case updateEvt:
if ((WindowPtr) theEvent.message == theWindow) {
UpdateWindow();
return 0;
}
else {
*updateWindow=(WindowPtr) theEvent.message;
*userCancel=0;
*deactivate=0;
return 1;
}
break;
case activateEvt:
if (theEvent.modifiers & activeFlag) {
SetCursor(&arrow);
HiliteControl(cancelButton, 0);
return 0;
}
else {
HiliteControl(cancelButton, 255);
if ((WindowPtr) theEvent.message!=theWindow) {
*deactivate=1;
*userCancel=0;
*updateWindow=(WindowPtr) theEvent.message;
return 1;
}
return 0;
}
break;
case diskEvt:
if (HiWord(theEvent.message)!=noErr) {
Point thisPoint;
DILoad();
SetPt(&thisPoint, 120, 120);
DIBadMount(thisPoint, theEvent.message);
DIUnload();
return 0;
}
default:
return 0;
break;
}
}
return 0;
}
void ProgressClass::DeselectItems(short menuID)
{
MenuHandle theMenu=GetMHandle(menuID);
if (theMenu) {
DisableItem(theMenu, 0);
DrawMenuBar();
}
}
void ProgressClass::DimMenus()
{
if (menu1)
DeselectItems(menu1);
if (menu2)
DeselectItems(menu2);
if (menu3)
DeselectItems(menu3);
if (menu4)
DeselectItems(menu4);
if (menu5)
DeselectItems(menu5);
if (menu6)
DeselectItems(menu6);
if (menu7)
DeselectItems(menu7);
if (menu8)
DeselectItems(menu8);
}
void ProgressClass::SelectItems(short menuID)
{
MenuHandle theMenu=GetMHandle(menuID);
if (theMenu) {
EnableItem(theMenu, 0);
DrawMenuBar();
}
}
void ProgressClass::HiliteMenus()
{
if (menu1)
SelectItems(menu1);
if (menu2)
SelectItems(menu2);
if (menu3)
SelectItems(menu3);
if (menu4)
SelectItems(menu4);
if (menu5)
SelectItems(menu5);
if (menu6)
SelectItems(menu6);
if (menu7)
SelectItems(menu7);
if (menu8)
SelectItems(menu8);
}